@echo off
chcp 65001

:: Zenith Mind 微服务部署脚本 (Windows)
echo ==========================================
echo Zenith Mind 微服务 Docker 部署脚本 (Windows)
echo ==========================================

:: 检查Docker是否安装
docker --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker 未安装或未正确配置
    pause
    exit /b 1
)

:: 检查Docker Compose是否安装
docker-compose --version >nul 2>&1
if errorlevel 1 (
    docker compose version >nul 2>&1
    if errorlevel 1 (
        echo 错误: Docker Compose 未安装
        pause
        exit /b 1
    )
)

:menu
cls
echo.
echo 请选择操作:
echo 1) 构建所有服务
echo 2) 启动所有服务
echo 3) 停止所有服务
echo 4) 重启所有服务
echo 5) 查看服务状态
echo 6) 查看日志
echo 7) 清理所有容器和数据
echo 0) 退出
echo.

set /p choice=请输入选项 (0-7): 

if "%choice%"=="1" goto build_all
if "%choice%"=="2" goto start_all
if "%choice%"=="3" goto stop_all
if "%choice%"=="4" goto restart_all
if "%choice%"=="5" goto show_status
if "%choice%"=="6" goto show_logs
if "%choice%"=="7" goto clean_all
if "%choice%"=="0" goto exit

echo 无效选项，请重新输入
timeout /t 2 >nul
goto menu

:build_all
echo 正在构建所有服务...

:: 检查Maven Wrapper
cd zenith-gateway
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

cd zenith-user-service
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

cd zenith-api-service
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

cd zenith-edu-service
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

cd zenith-article-service
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

cd zenith-middleware-service
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests
) else (
    call mvn clean package -DskipTests
)
cd ..

echo 所有服务构建完成
pause
goto menu

:start_all
echo 正在启动所有服务...
docker-compose up -d
if errorlevel 1 (
    echo 服务启动失败
) else (
    echo 所有服务启动成功
    echo 等待服务启动完成...
    timeout /t 30 >nul
    docker-compose ps
)
pause
goto menu

:stop_all
echo 正在停止所有服务...
docker-compose down
echo 所有服务已停止
pause
goto menu

:restart_all
echo 正在重启所有服务...
docker-compose restart
echo 所有服务已重启
pause
goto menu

:show_status
echo 服务状态:
docker-compose ps
pause
goto menu

:show_logs
echo 选择查看日志的服务:
echo 1) 所有服务
echo 2) 网关服务 (zenith-gateway)
echo 3) 用户服务 (zenith-user-service)
echo 4) API服务 (zenith-api-service)
echo 5) 教育服务 (zenith-edu-service)
echo 6) 文章服务 (zenith-article-service)
echo 7) 中间件服务 (zenith-middleware-service)
echo 8) Nacos
echo 9) MySQL
echo 10) Redis

set /p log_choice=请输入选项 (1-8): 

if "%log_choice%"=="1" docker-compose logs -f
if "%log_choice%"=="2" docker-compose logs -f zenith-gateway
if "%log_choice%"=="3" docker-compose logs -f zenith-user-service
if "%log_choice%"=="4" docker-compose logs -f zenith-api-service
if "%log_choice%"=="5" docker-compose logs -f zenith-edu-service
if "%log_choice%"=="6" docker-compose logs -f zenith-article-service
if "%log_choice%"=="7" docker-compose logs -f zenith-middleware-service
if "%log_choice%"=="8" docker-compose logs -f nacos
if "%log_choice%"=="9" docker-compose logs -f mysql
if "%log_choice%"=="10" docker-compose logs -f redis

pause
goto menu

:clean_all
echo 警告: 这将删除所有容器和数据卷
set /p confirm=确定要继续吗? (y/N): 

if /i "%confirm%"=="y" (
    echo 正在清理所有容器和数据...
    docker-compose down -v
    docker system prune -f
    echo 清理完成
) else (
    echo 取消清理操作
)
pause
goto menu

:exit
echo 退出脚本
exit /b 0